package mirfordevoservice.system.req.impl;

import mirfordevoservice.system.GameCenter.GameDataManager;
import mirfordevoservice.system.req.inter_face.ReqMessage;
import publicPackage.human.Role;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import static mirfordevoservice.system.contasnt.GameConstant.GameSession.ONLINE_ROLE_SESSION;

/**
 * Created by Heart!
 *
 * @Author ZhangChaoJian
 * @Date ${Date}-11:47
 * Wants More Then Try Hard More!
 */

/**
 * 登陆请求处理
 */
public class ReqLoginToGame implements ReqMessage {


    @Override
    public void receive(ObjectInputStream in, Socket socket) {

    }

    @Override
    public void sendMsg(ObjectOutputStream out) {

    }

    @Override
    public void receiveAndSend(ObjectInputStream in, ObjectOutputStream out, Socket socket) {
        Role role = null;
        try {
            Object msgOne = in.readObject();
            Object msgTwo = in.readObject();
            if ((msgOne instanceof String) && (msgTwo instanceof char[])) {
                String count = (String) msgOne;
                char[] password = (char[]) msgTwo;
                role = GameDataManager.getInstance().CheckForLoginToSelectPage(count, password);
                if (role == null) {
                    System.err.println("无法获取登陆信息");
                    return;
                }
                for (Socket other : ONLINE_ROLE_SESSION.keySet()) {
                    if (ONLINE_ROLE_SESSION.get(other) == role.getUserID()) {
                        GameDataManager.getInstance().reConnected(other);
                        break;
                    }
                }
                ONLINE_ROLE_SESSION.put(socket, role.getUserID());//存入在线人物会话
                System.out.println("存入会话，玩家" + role.getUser_count() + "上线了...");
            } else {
                System.out.println("客户端发送数据类型有误");
            }
            if (role == null) {
                out.writeObject(false);
                System.err.println("角色数据无法获取");
                return;
            }
            out.writeObject(role);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
